package net.adetech.crj.dao;

import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Model;
import net.adetech.crj.utils.SerializeUtil;
import net.adetech.crj.utils.ServiceUtil;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Queue extends Model<Queue> {
    public static final Queue dao = new Queue().dao();

    private static boolean async = PropKit.getBoolean("async",false);


    @Override
    public boolean save() {
        boolean result = super.save();
        if (result&&async){
            Map<String,Object> params = new HashMap<>();
            params.put("queue",this.getBean());
            ServiceUtil.callAsync("async.insertQueue",params);
        }
        return result;
    }


    @Override
    public boolean update() {
        boolean result = super.update();
        if (result&&async){
            Map<String,Object> params = new HashMap<>();
            params.put("queue",this.getBean());
            ServiceUtil.callAsync("async.updateQueue",params);
        }
        return result;
    }

    private net.adetech.crj.entity.Queue getBean ()
    {
        String queueId = this.getStr("QUEUE_ID");
        String queueNumber = this.getStr("QUEUE_NUMBER");
        String serviceZone = this.getStr("SERVICE_ZONE");
        String serviceType = this.getStr("SERVICE_TYPE");
        String idType = this.getStr("IDENTITY_TYPE");
        String idNumber = this.getStr("IDENTITY_NUMBER");
        String realName = this.getStr("REAL_NAME");
        String realNameEn = this.getStr("REAL_NAME_EN");
        String state = this.getStr("STATE");
        Timestamp createTime = this.getTimestamp("CREATE_TIME");
        Timestamp updateTime = this.getTimestamp("UPDATE_TIME");
        String dayId = this.getStr("DAY_ID");
        String openId = this.getStr("OPEN_ID");
        String wxName = this.getStr("WX_NAME");
        String wxIconUrl = this.getStr("WX_ICON_URL");
        String nextServiceType = this.getStr("NEXT_SERVICE_TYPE");
        String windowName = this.getStr("WINDOW_NAME");
        int waitPerson = this.getInt("WAIT_PERSON");
        String vipType = this.getStr("VIP_TYPE");
        String tableState = this.getStr("TABLE_STATE");
        String printState = this.getStr("PRINT_STATE");
        String policeNo = this.getStr("POLICE_NO");
        net.adetech.crj.entity.Queue bean = new net.adetech.crj.entity.Queue();
        bean.setQueueId(queueId);
        bean.setQueueNumber(queueNumber);
        bean.setServiceZone(serviceZone);
        bean.setServiceType(serviceType);
        bean.setIdentityType(idType);
        bean.setIdentityNumber(idNumber);
        bean.setRealName(realName);
        bean.setRealNameEn(realNameEn);
        bean.setState(state);
        bean.setCreateTime(createTime);
        bean.setUpdateTime(updateTime);
        bean.setDayId(dayId);
        bean.setOpenId(openId);
        bean.setWxName(wxName);
        bean.setWxIconUrl(wxIconUrl);
        bean.setNextServiceType(nextServiceType);
        bean.setWindowName(windowName);
        bean.setWaitPerson(waitPerson);
        bean.setVipType(vipType);
        bean.setTableState(tableState);
        bean.setPrintState(printState);
        bean.setPoliceNo(policeNo);
        return bean;
    }


    public static Queue getQueueByNumber(String number, String dayId) {
        List<Queue> queueList = dao.find("select * from CRJ_QUEUE where DAY_ID = ? and QUEUE_NUMBER = ?",dayId,number);
        if (queueList!=null&&queueList.size()>0)
        {
            return queueList.get(0);
        }
        return null;
    }

    public static Queue getCurrentNo(String zone) {
        String dayId = new SimpleDateFormat("yyyyMMdd").format(new Date());
        List<Queue> queueList = dao.find("select * from CRJ_QUEUE where DAY_ID = ? and STATE = '2' and SERVICE_ZONE = ? ",dayId,zone);
        if (queueList!=null&&queueList.size()>0)
        {
            return queueList.get(0);
        }
        return null;
    }


    public static List<Queue> getTodayQueueList()
    {
        String dayId = new SimpleDateFormat("yyyyMMdd").format(new Date());
        List<Queue> queueList = dao.find("select * from CRJ_QUEUE where DAY_ID = ?",dayId);
        return  queueList;
    }

    public static Queue getQueueByNumber(String number) {
        String dayId = new SimpleDateFormat("yyyyMMdd").format(new Date());
        return getQueueByNumber(number,dayId);
    }



//    public Queue encodeEntity()
//    {
//        String realName = this.getStr("REAL_NAME");
//        String idNumber = this.getStr("ID_NUMBER");
//        String wxName = this.getStr("WX_NAME");
//        realName = SerializeUtil.encodeBase64(realName);
//        idNumber = SerializeUtil.encodeBase64(idNumber);
//        wxName = SerializeUtil.encodeBase64(wxName);
//        this.set("REAL_NAME",realName);
//        this.set("ID_NUMBER",idNumber);
//        this.set("WX_NAME",wxName);
//
//        return  this;
//    }
//
//    public Queue decodeEntity()
//    {
//        String realName = this.getStr("REAL_NAME");
//        String idNumber = this.getStr("ID_NUMBER");
//        String wxName = this.getStr("WX_NAME");
//        realName = SerializeUtil.decodeBase64(realName);
//        idNumber = SerializeUtil.decodeBase64(idNumber);
//        wxName = SerializeUtil.decodeBase64(wxName);
//        this.set("REAL_NAME",realName);
//        this.set("ID_NUMBER",idNumber);
//        this.set("WX_NAME",wxName);
//
//        return  this;
//    }
}
